// pages/article/article.js
const app = getApp();
const db = wx.cloud.database();
const _ = db.command;
// const util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {

    disabled: true,
    title: '',
    nickname: "",
    time: "",
    tag: "",
    content: [],
    images: "",
    view: "",
    zan: "",
    number: "",
    comments: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    db.collection('article').where({
      _id: options.id
    }).update({
      data: {
        view: _.inc(1)
      }
    }).then(res => {
      console.log(res, '+1');
    })
    if (app.globalData.userInfo) {
      this.setData({
        disabled: false
      })
    }
    wx.cloud.callFunction({
      name: 'getarticle',
      data: {
        _id: options.id
      },
      complete: res => {
        wx.setStorageSync('articleData', res.result.data[0])
        const { title,
          nickname,
          time,
          tag,
          contents, images, view, zan, number, comments, _id } = res.result.data[0]
        this.setData({
          title: title,
          nickname: nickname,
          time: time,
          tag: tag,
          content: contents,
          images: images,
          view: view,
          zan: zan,
          number: number,
          comments: comments,
          _id: _id
        })
      }
    })
  },
  increaseNumberOne(e) {
    db.collection('article').where({
      _id: e.currentTarget.dataset.id
    }).update({
      data: {
        zan: _.inc(1)
      }
    }).then(res => {
      console.log(res, '+1');
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})